package edu.zqu.collegeField.project.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import edu.zqu.collegeField.common.exception.MyException;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.dto.AreaDto;
import edu.zqu.collegeField.project.po.Area;
import edu.zqu.collegeField.project.mapper.AreaMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.project.service.AreaService;
import edu.zqu.collegeField.project.vo.AreaVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 功能区 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;


    @Override
    public List<AreaVo> get(BigInteger collegeId,//获取功能区列表
                            BigInteger schoolId,
                            String name) {
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("schoolId", Convert.toStr(schoolId));
        map.put("name", name);
        List<AreaVo> areaVoList = BeanMapper.mapList(areaMapper.get(map), AreaVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (AreaVo areaVo : areaVoList) {
            areaVo.setCollegeName(collegeName);
        }
        return areaVoList;
    }

    @Override
    public int add(Area area) {
        area.setId(null).setUpdateTime(new Timestamp(System.currentTimeMillis())).setDeleted(null);
        return areaMapper.insert(area);
    }

    @Override
    public AreaVo getById(BigInteger id) {//获取功能区
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        AreaVo areaVo = BeanMapper.map(areaMapper.getById(map), AreaVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(areaVo.getCollegeId());
        areaVo.setCollegeName(collegeName);
        return areaVo;
    }

    @Override
    public AreaVo getByName(BigInteger collegeId, String name) {//获取功能区
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("name", StrUtil.trimToNull(name));
        AreaVo areaVo = BeanMapper.map(areaMapper.getByName(map), AreaVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        areaVo.setCollegeName(collegeName);
        return areaVo;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(Area areaUpdate) {//更新功能区
        Area area = areaMapper.selectById(areaUpdate.getId());
        if (StringUtils.isNull(area)) {
            throw new NullPointerException("找不到该功能区");
        }
        BeanMapper.copyPropertiesNotNull(areaUpdate, area);
        try {
            int insert = areaMapper.insert(area.setLastVersion(areaUpdate.getId()));
            if (insert >= 1) {
                return delete(areaUpdate.getId(), areaUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {
        try {
            UpdateWrapper<Area> areaUpdateWrapper = new UpdateWrapper<Area>().eq("id", Convert.toStr(id));
            int update = areaMapper.update(new Area().setByWho(byWho), areaUpdateWrapper);
            if (update >= 1) {
                return areaMapper.delete(areaUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
